<template>
    <div class="login_container">
        <div class="background">
            <img src="../assets/images/background.png" width="100%" height="100%" alt="这是一张背景图!!!" />
        </div>
        <div class="front"></div>
        <div class="login_box">
            <div class="avatar_box">
                <el-form ref="loginForm" :rules="loginRules" :model="loginForm" label-width="0px" class="login_form">
                    <el-form-item prop="username">
                        <el-input v-model="loginForm.username" prefix-icon="iconfont icon-denglu-copy"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input v-model="loginForm.password" type="password" prefix-icon="iconfont icon-denglu-mima">
                        </el-input>
                    </el-form-item>
                    <el-form-item class="btns">
                        <el-button type="primary" @click="login">提交</el-button>
                        <el-button type="info" @click="resetLoginForm()">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                loginForm: {
                    username: "18037300759",
                    password: "123456"
                },
                loginRules: {
                    username: [
                        { required: true, message: '请输入账号', trigger: 'blur' },
                        { min: 5, max: 11, message: '长度为 5 ~ 11 个字符', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 6, max: 24, message: '长度在 6 ~ 24 个字符', trigger: 'blur' }
                    ],
                },
            }
        },
        methods: {
            resetLoginForm() {
                this.$refs.loginForm.resetFields();
            },
            login() {
                this.$refs.loginForm.validate(async validate => {
                    if (!validate) {
                        return;
                    }
                    const { data: res } = await this.$http.post("/door/login", this.loginForm);
                    if (res.code === 200) {
                        this.$message.success("登录成功!");
                        window.sessionStorage.setItem("Authorization", res.data.token);
                        window.sessionStorage.setItem("userMenus", JSON.stringify(res.data.menus));
                        this.$router.push("/index");
                    } else {
                        this.$message.error(res.message);
                    }
                })
            },
        }

    }
</script>

<style lang="less" scoped>
    .background {
        width: 100%;
        height: 100%;
        z-index: -1;
        position: absolute;
    }

    .front {
        z-index: 1;
        position: absolute;
    }

    .login_container {
        height: 100%;
    }

    .login_box {
        width: 350px;
        height: 400px;
        background-color: rgba(16, 17, 43, 0.404);
        border-radius: 4px;
        position: absolute;
        left: 72%;
        top: 50%;
        transform: translate(-50%, -50%);
    }

    .btns {
        text-align: center;
    }

    .login_form {
        position: absolute;
        bottom: 0%;
        width: 100%;
        top: 20%;
        padding: 0 10px;
        box-sizing: border-box;
    }
</style>